/*
 *  This file is part of WWWPal.
 *
 *  Copyright (C) 2003  Dr. John Punin, Department of Computer Science
 *  Rensselaer Polytechnic Institute
 *
 *  WWWPal is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  WWWPal is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "rep_logs_pages.h"
#include "rep_general.h"
#include "rep_blinks.h"
#include "GR_write_html.h"

void
write_body_pmreq_html_file(GraphFrame *gf,FILE *fp,int npages,WebStatPtr msn);

void
write_body_sma_html_file(GraphFrame *gf,FILE *fp,int npages,WebStatPtr msn);

void
write_body_bma_html_file(GraphFrame *gf,FILE *fp,int nbrowsers,WebStatPtr msn);

void
write_body_spmreq_html_file(GraphFrame *gf,FILE *fp,int npages,WebStatPtr msn);

int 
report_pages_most_requested(GraphFrame *gf, char *name_file,int npages,
			    WebStatPtr msn)
{
  FILE *fp = assert_open_html_file(name_file);
  if(!fp)
      return -1;
  write_head_html_file(fp,"Most Requested Pages");
  write_start_body_html_file(fp,"#ffffff");
  write_body_pmreq_html_file(gf,fp,npages,msn);
  write_end_body_html_file(fp);
  write_end_html_file(fp);
  fclose(fp);
  return 0;
}

void
write_time_periods(FILE *fp,WebStatPtr msn)
{
    time_t tcsd = LOGR_get_minimum_start_date(msn);
    time_t tced = LOGR_get_maximum_end_date(msn);
    char csd[100];
    char ced[100];

    if(!msn || !fp)
	return;

    HTBufferTimeApache(tcsd,csd,100);
    HTBufferTimeApache(tced,ced,100);

    write_blank_spaces_html_file(fp,4);
    write_bold_html_file(fp,"Time Period : ");
    fprintf(fp, " %s - %s",csd,ced);
    write_break_html_file(fp);
}

void
write_body_pmreq_html_file(GraphFrame *gf,FILE *fp,int npages,WebStatPtr msn)
{
  LNode_type *nodes = get_list_vertices(gf);
  LNode_type *ptr = nodes->back;
  PNODE *varray = NULL;
  PNODE *varray2 = NULL;
  PNODE *varray3 = NULL;
  int i=0,j=0,k=0;
  char ntitle[100];
  int tnpages=0;

  time_t now;
  int nobj = 0;
  int nhtml = 0;
  int nnhtml = 0;
  long thits = LOGR_get_total_site_naccesses(msn);
  
  for(; ptr != nodes; ptr=ptr->back)
  {
    PNODE v = (PNODE)ptr->info;
    const char *smime = get_attribute(v, "mime");

    if(get_counter(v)== 0)
	continue;

    if(smime)
    {
      if(is_vertex_object(v))
	nobj++;
      else if(!strcmp(smime,"text/html"))
	nhtml++;
      else
	nnhtml++;
    }
  }

  varray = nhtml ? (PNODE *)calloc(nhtml,sizeof(PNODE)) : NULL;

  if(!varray && nhtml > 0) {
      fprintf(stderr,"Out of Memory write_body_pmreq_html_file\n");
      abort();
  }
  varray2 = nnhtml ? (PNODE *)calloc(nnhtml,sizeof(PNODE)) : NULL;

  if(!varray2 && nnhtml > 0) {
      fprintf(stderr,"Out of Memory write_body_pmreq_html_file\n");
      abort();
  }

  varray3 = nobj ? (PNODE *)calloc(nobj,sizeof(PNODE)) : NULL;

  if(!varray3 && nobj > 0) {
      fprintf(stderr,"Out of Memory write_body_pmreq_html_file\n");
      abort();
  }

  for(ptr = nodes->back; ptr != nodes; ptr=ptr->back)
  {
    PNODE v = (PNODE)ptr->info;
    const char *smime = get_attribute(v, "mime");

    if(get_counter(v)== 0)
	continue;

    if(smime)
    {
      if(is_vertex_object(v))
	varray3[k++] = v;
      else if(!strcmp(smime,"text/html"))
	varray[i++] = v;
      else
	varray2[j++] = v;
    }
  }

  now = time(NULL);
  write_header_html_file(fp,1,"List of Most Requested Pages");
  write_bold_html_file(fp,"Last Update :");
  fprintf(fp, " %s",ctime(&now));
  write_break_html_file(fp);
  write_bold_html_file(fp,"Time Periods : ");
  write_break_html_file(fp);
  write_time_periods(fp,msn);
  write_bold_html_file(fp,"Total Number of Requests : ");
  fprintf(fp, " %ld \n",thits);
  write_horizontal_rule_html_file(fp);
  if(nobj > 0)
      fprintf(fp,RMRPString);
  else
      fprintf(fp,RMRPString2);
  write_horizontal_rule_html_file(fp);

  if(nhtml > 0) {
  if(npages > nhtml)
      tnpages = nhtml;
  else
      tnpages = npages;

  sprintf(ntitle,"%d Most Requested HTML Pages", tnpages);
  write_header_html_file(fp,2,ntitle);
  write_start_simple_table_html_file(fp);
  write_header_table_string4(fp,"Num", "URL","Hits","Percentage","Size");
  for(i = 1; i<=tnpages ; i++)
  {
    PNODE v = (PNODE)randomized_select((GPTR *)varray,i-1,nhtml-1,1,
			  is_counter_lower,is_counter_bigger);
    if(v)
    {
	char *vlabel = get_label_vertex(v);
	int vcount = get_counter(v);
	char *tit = (char *)get_attribute(v,"title");
	char *sbytes = get_weight_vertex(v);
	float ave_nacc = ((float)vcount/(float)thits)*100.0;
	char snum[20],scount[20],save[20];
	if(!sbytes) sbytes = "0";
	sprintf(snum,"%d",i);
	sprintf(scount,"%d",vcount);
	sprintf(save,"%.2f %s",ave_nacc,"&#37;");
	write_row_num_link_title_string3(fp,snum,vlabel,tit,scount,save,
					 sbytes);
    }
  }
  write_end_table_html_file(fp); }

  if(nnhtml > 0) {
  if(npages > nnhtml)
      tnpages = nnhtml;
  else
      tnpages = npages;

  sprintf(ntitle,"%d Most Requested Non HTML Pages", tnpages);
  write_header_html_file(fp,2,ntitle);
  write_start_simple_table_html_file(fp);
  write_header_table_string4(fp,"Num","URL","Hits","Percentage","Size");
  for(i = 1; i<=tnpages ; i++)
  {
    PNODE v = (PNODE)randomized_select((GPTR *)varray2,i-1,nnhtml-1,1,
			  is_counter_lower,is_counter_bigger);
    if(v)
    {
	char *vlabel = get_label_vertex(v);
	int vcount = get_counter(v);
	float ave_nacc = ((float)vcount/(float)thits)*100.0;
	char *sbytes = get_weight_vertex(v);
	char snum[20],scount[20],save[20];
	if(!sbytes) sbytes = "0";
	sprintf(snum,"%d",i);
	sprintf(scount,"%d",vcount);
	sprintf(save,"%.2f %s",ave_nacc,"&#37;");
	write_row_num_link_string3(fp,snum,vlabel,scount,save,sbytes);
    }
  }
  write_end_table_html_file(fp); }

  if(nobj > 0) {
  if(npages > nobj)
      tnpages = nobj;
  else
      tnpages = npages;

  sprintf(ntitle,"%d Most Requested Inline Objects", tnpages);
  write_header_html_file(fp,2,ntitle);
  write_start_simple_table_html_file(fp);
  write_header_table_string4(fp,"Num","URL","Hits","Percentage","Size");

  for(i = 1; i<=tnpages ; i++)
  {
    PNODE v = (PNODE)randomized_select((GPTR *)varray3,i-1,nobj-1,1,
			  is_counter_lower,is_counter_bigger);
    if(v)
    {
	char *vlabel = get_label_vertex(v);
	int vcount = get_counter(v);
	float ave_nacc = ((float)vcount/(float)thits)*100.0;
	char *sbytes = get_weight_vertex(v);
	char snum[20],scount[20],save[20];
	if(!sbytes) sbytes = "0";
	sprintf(snum,"%d",i);
	sprintf(scount,"%d",vcount);
	sprintf(save,"%.2f %s",ave_nacc,"&#37;");
	write_row_num_link_string3(fp,snum,vlabel,scount,save,sbytes);
    }
  }
  write_end_table_html_file(fp); }

  free(varray); varray = NULL;
  free(varray2); varray2 = NULL;
  free(varray3); varray3 = NULL;
}

int 
report_sites_most_accesses(GraphFrame *gf, char *name_file,int nsites,
			    WebStatPtr msn)
{
  FILE *fp = assert_open_html_file(name_file);
  if(!fp)
      return -1;
  write_head_html_file(fp,"Sites with Most Access Count ");
  write_start_body_html_file(fp,"#ffffff");
  write_body_sma_html_file(gf,fp,nsites,msn);
  write_end_body_html_file(fp);
  write_end_html_file(fp);
  fclose(fp);
  return 0;
}

void
write_body_sma_html_file(GraphFrame *gf,FILE *fp,int nsites,WebStatPtr msn)
{
  LNode_type *lsites = LOGR_get_list_sites(msn);
  LNode_type *ptr = NULL;
  WebSiteNodePtr *varray = NULL;
  WebSiteNodePtr *varray2 = NULL;
  int i=0;
  int j=0;
  char ntitle[100];
  int tn=0;
  int tnsites = lsites ? count_llist(lsites) : 0;
  int tnsloc = 0;
  int tnsext = 0;
  double tsize = LOGR_get_total_site_size(msn);
  long tacc = LOGR_get_total_site_naccesses(msn);
  time_t now;

  for(ptr = lsites->back; ptr != lsites; ptr=ptr->back)
  {
    WebSiteNodePtr msite = (WebSiteNodePtr)ptr->info;
    const char *lsite = LOGR_get_site_dns(msite);
    const char *lip = LOGR_get_site_ip(msite);
    short islocal = 0;

    if(lsite)
	islocal = LOGR_is_address_local(msn,(char *)lsite);
    else if(lip)
	islocal = LOGR_is_address_local(msn,(char *)lip);

    if(islocal)
	tnsloc++;
    else
	tnsext++;
  }  

  varray = tnsloc ? (WebSiteNodePtr *)calloc(tnsloc,sizeof(WebSiteNodePtr)) : NULL;

  if(!varray && tnsloc > 0) {
      fprintf(stderr,"Out of Memory write_body_sma_html_file\n");
      abort();
  }

  varray2 = tnsext ? (WebSiteNodePtr *)calloc(tnsext,sizeof(WebSiteNodePtr)) : NULL;

  if(!varray2 && tnsext > 0) {
      fprintf(stderr,"Out of Memory write_body_sma_html_file\n");
      abort();
  }

  for(ptr = lsites->back; ptr != lsites; ptr=ptr->back)
  {
    WebSiteNodePtr msite = (WebSiteNodePtr)ptr->info;
    const char *lsite = LOGR_get_site_dns(msite);
    const char *lip = LOGR_get_site_ip(msite);
    short islocal = 0;
    if(lsite)
	islocal = LOGR_is_address_local(msn,(char *)lsite);
    else if(lip)
	islocal = LOGR_is_address_local(msn,(char *)lip);

    if(islocal)
	varray[i++] = msite;
    else
	varray2[j++] = msite;
  }

  now = time(NULL);
  write_header_html_file(fp,1,"List of Sites with Most Access Count");
  write_bold_html_file(fp,"Last Update :");
  fprintf(fp, " %s",ctime(&now));
  write_break_html_file(fp);
  write_bold_html_file(fp,"Time Periods : ");  
  write_break_html_file(fp);
  write_time_periods(fp,msn);
  write_bold_html_file(fp,"Unique Sites : ");
  fprintf(fp, " %d Sites\n",tnsites);
  write_break_html_file(fp);
  write_bold_html_file(fp,"Local Sites : ");
  fprintf(fp, " %d Sites\n",tnsloc);
  write_break_html_file(fp);
  write_bold_html_file(fp,"External Sites : ");
  fprintf(fp, " %d Sites\n",tnsext);
  write_break_html_file(fp);
  write_bold_html_file(fp,"Total Number of Access Count : ");
  fprintf(fp, " %ld \n",tacc);
  write_break_html_file(fp);
  write_bold_html_file(fp,"Total Size : ");
  fprintf(fp, " %.2f K Bytes\n",(float)(tsize/1024.0));
  write_horizontal_rule_html_file(fp);
  fprintf(fp,RSMAString);
  write_horizontal_rule_html_file(fp);

  if(tnsloc > 0) {
      
  if(nsites < tnsloc)
      tn = nsites;
  else
      tn = tnsloc;

  sprintf(ntitle,"%d Local Sites with Most Access Count", tn);
  write_header_html_file(fp,2,ntitle);
  write_start_simple_table_html_file(fp);
  write_header_table_string6(fp,"Num","Site","Access Count",
			     "Percentage Access Count","HTML Pages",
			     "Bytes","Percentage Bytes");
  for(i = 1; i<=tn; i++)
  {
    WebSiteNodePtr msite= (WebSiteNodePtr)
	randomized_select((GPTR *)varray,i-1,tnsloc-1,1,
			  LOGR_is_access_lower,LOGR_is_access_bigger);
    if(msite)
    {
	const char *lsite = LOGR_get_site_dns(msite);
	const char *lip = LOGR_get_site_ip(msite);
	long scount = LOGR_get_site_naccesses(msite);
	long npages = LOGR_get_site_nacc_search(msite);
	char snum[20],str[20], str2[20], str3[20], str4[20],str5[20];
	long ssize = LOGR_get_site_sizebytes(msite);
	double ave_size = (double)(ssize/tsize)*100.0;
	float ave_nacc = ((float)scount/(float)tacc)*100.0;
	sprintf(snum,"%d",i);
	sprintf(str,"%ld",scount);
	sprintf(str5,"%ld",npages);
	sprintf(str2,"%.2f K",(float)(ssize/1024.0));
	sprintf(str3,"%.2f %s",(float)(ave_size),"&#37;"); 
	sprintf(str4,"%.2f %s",ave_nacc,"&#37;"); 
	if(lsite)
	    write_row_num_string6(fp,snum,(char *)lsite,str,str4,str5,str2,str3);
	else if(lip)
	    write_row_num_string6(fp,snum,(char *)lip,str,str4,str5,str2,str3);
    }
  }
  write_end_table_html_file(fp); }


  if(tnsext > 0) {
      
  if(nsites < tnsext)
      tn = nsites;
  else
      tn = tnsext;

  sprintf(ntitle,"%d External Sites with Most Access Count", tn);
  write_header_html_file(fp,2,ntitle);
  write_start_simple_table_html_file(fp);
  write_header_table_string6(fp,"Num","Site","Access Count",
			     "Percentage Access Count","HTML Pages",
			     "Bytes","Percentage Bytes");
  for(i = 1; i<=tn; i++)
  {
    WebSiteNodePtr msite= (WebSiteNodePtr)
	randomized_select((GPTR *)varray2,i-1,tnsext-1,1,
			  LOGR_is_access_lower,LOGR_is_access_bigger);
    if(msite)
    {
	const char *lsite = LOGR_get_site_dns(msite);
	const char *lip = LOGR_get_site_ip(msite);
	long scount = LOGR_get_site_naccesses(msite);
	long npages = LOGR_get_site_nacc_search(msite);
	char snum[20],str[20], str2[20], str3[20], str4[20],str5[20];
	long ssize = LOGR_get_site_sizebytes(msite);
	double ave_size = (double)(ssize/tsize)*100.0;
	float ave_nacc = ((float)scount/(float)tacc)*100.0;
	sprintf(snum,"%d",i);
	sprintf(str,"%ld",scount);
	sprintf(str5,"%ld",npages);
	sprintf(str2,"%.2f K",(float)(ssize/1024.0));
	sprintf(str3,"%.2f %s",(float)(ave_size),"&#37;"); 
	sprintf(str4,"%.2f %s",ave_nacc,"&#37;"); 
	if(lsite)
	    write_row_num_string6(fp,snum,(char *)lsite,str,str4,str5,str2,str3);
	else if(lip)
	    write_row_num_string6(fp,snum,(char *)lip,str,str4,str5,str2,str3);
    }
  }
  write_end_table_html_file(fp); }

  if(varray) {
      free(varray); varray = NULL; 
  }
  if(varray2) {
      free(varray2); varray2 = NULL;
  }

}

int 
report_pages_most_browsers_requested(GraphFrame *gf, char *name_file,
				     int nbrowsers,WebStatPtr msn)
{
  FILE *fp = assert_open_html_file(name_file);
  if(!fp)
      return -1;
  write_head_html_file(fp,"Browsers with Most Requests ");
  write_start_body_html_file(fp,"#ffffff");
  write_body_bma_html_file(gf,fp,nbrowsers,msn);
  write_end_body_html_file(fp);
  write_end_html_file(fp);
  fclose(fp);
  return 0;
}

void
write_body_bma_html_file(GraphFrame *gf,FILE *fp,int nuas,WebStatPtr msn)
{
  LNode_type *luas = LOGR_get_list_ua(msn);
  LNode_type *lbrowsers = LOGR_get_list_browser(msn);
  LNode_type *lplatforms = LOGR_get_list_platform(msn);
  LNode_type *lspiders = LOGR_get_list_spider(msn);
  LNode_type *ptr=NULL;
  WebBrowserNodePtr *varray = NULL;
  WebBrowserNodePtr *varray2 = NULL;
  WebBrowserNodePtr *varray3 = NULL;
  WebBrowserNodePtr *varray4 = NULL;
  int i=0,j=0,k=0,l=0;
  char ntitle[100];
  int tn=0;
  int tnuas = luas ? count_llist(luas) : 0;
  int tnbrowsers = lbrowsers ? count_llist(lbrowsers) : 0;
  int tnplatforms = lplatforms ? count_llist(lplatforms) : 0;
  int tnspiders = lspiders ? count_llist(lspiders) : 0;
  double tsize = LOGR_get_total_browser_size(msn);
  long tacc = LOGR_get_total_site_naccesses(msn);
  time_t now;
  
  varray = tnuas ? (WebBrowserNodePtr *)calloc(tnuas,sizeof(WebBrowserNodePtr)) : NULL;

  if(!varray && tnuas > 0) {
      fprintf(stderr,"Out of Memory write_body_bma_html_file\n");
      abort();
  }

  for(ptr = luas->back; ptr != luas; ptr=ptr->back)
  {
    WebBrowserNodePtr mua = (WebBrowserNodePtr)ptr->info;
    varray[i++] = mua;
  }

  varray2 = tnbrowsers ? (WebBrowserNodePtr *)calloc(tnbrowsers,sizeof(WebBrowserNodePtr)) : NULL;

  if(!varray2 && tnbrowsers > 0) {
      fprintf(stderr,"Out of Memory write_body_bma_html_file\n");
      abort();
  }

  for(ptr = lbrowsers->back; ptr != lbrowsers; ptr=ptr->back)
  {
    WebBrowserNodePtr mbrowser = (WebBrowserNodePtr)ptr->info;
    varray2[j++] = mbrowser;
  }


  varray3 = tnplatforms ? (WebBrowserNodePtr *)calloc(tnplatforms,sizeof(WebBrowserNodePtr)) : NULL;

  if(!varray3 && tnplatforms > 0) {
      fprintf(stderr,"Out of Memory write_body_bma_html_file\n");
      abort();
  }

  for(ptr = lplatforms->back; ptr != lplatforms; ptr=ptr->back)
  {
    WebBrowserNodePtr mplatform = (WebBrowserNodePtr)ptr->info;
    varray3[k++] = mplatform;
  }

  varray4 = tnspiders ? (WebBrowserNodePtr *)calloc(tnspiders,sizeof(WebBrowserNodePtr)) : NULL;

  if(!varray4 && tnspiders > 0) {
      fprintf(stderr,"Out of Memory write_body_bma_html_file\n");
      abort();
  }

  for(ptr = lspiders->back; ptr != lspiders; ptr=ptr->back)
  {
    WebBrowserNodePtr mspider = (WebBrowserNodePtr)ptr->info;
    varray4[l++] = mspider;
  }

  now = time(NULL);
  write_header_html_file(fp,1,"List of Browsers with Most Access Count");
  write_bold_html_file(fp,"Last Update :");
  fprintf(fp, " %s",ctime(&now));
  write_break_html_file(fp);
  write_bold_html_file(fp,"Time Periods : ");  
  write_break_html_file(fp);
  write_time_periods(fp,msn);
  write_bold_html_file(fp,"Unique User Agents (Browsers and Spiders) : ");
  fprintf(fp, " %d User Agents\n",tnuas);
  write_break_html_file(fp);
  write_bold_html_file(fp,"Total Number of Access Count : ");
  fprintf(fp, " %ld \n",tacc);
  write_break_html_file(fp);
  write_bold_html_file(fp,"Total Size : ");
  fprintf(fp, " %.2f K Bytes\n",(float)(tsize/1024.0));
  write_horizontal_rule_html_file(fp);
  fprintf(fp,RBAString); 
  write_horizontal_rule_html_file(fp);

  if(tnbrowsers > 0) {
      tn = tnbrowsers;

  sprintf(ntitle,"Browser Class with Most Access Count");
  write_header_html_file(fp,2,ntitle);
  write_start_simple_table_html_file(fp);
  write_header_table_string5(fp,"Browser Class","Access Count",
			     "Percentage Access Count", "HTML Pages",
			     "Bytes","Percentage Bytes");
  for(i = 1; i<=tn; i++)
  {
    WebBrowserNodePtr mbrowser= (WebBrowserNodePtr)
	randomized_select((GPTR *)varray2,i-1,tnbrowsers-1,1,
			  LOGR_is_accessb_lower,LOGR_is_accessb_bigger);
    if(mbrowser)
    {
	const char *lbrowser = LOGR_get_user_agent(mbrowser);
	long scount = LOGR_get_browser_naccesses(mbrowser);
	long npages = LOGR_get_browser_npages(mbrowser);
	char str[20], str2[20], str3[20], str4[20],str5[20];
	double ssize = LOGR_get_browser_sizebytes(mbrowser);
	double ave_size = (ssize/tsize)*100.0;
	float ave_nacc = ((float)scount/(float)tacc)*100.0;
	sprintf(str,"%ld",scount);
	sprintf(str5,"%ld",npages);
	sprintf(str2,"%.2f K",ssize/1024.0);
	sprintf(str3,"%.2f %s",ave_size,"&#37;"); 
	sprintf(str4,"%.2f %s",ave_nacc,"&#37;"); 
	write_row_string5(fp,lbrowser,str,str4,str5,str2,str3);
    }
  }
  write_end_table_html_file(fp); }


  if(tnplatforms > 0) {
      tn = tnplatforms;

  sprintf(ntitle,"Platforms with Most Access Count");
  write_header_html_file(fp,2,ntitle);
  write_start_simple_table_html_file(fp);
  write_header_table_string5(fp,"Platform","Access Count",
			     "Percentage Access Count", "HTML Pages",
			     "Bytes","Percentage Bytes");
  for(i = 1; i<=tn; i++)
  {
    WebBrowserNodePtr mbrowser= (WebBrowserNodePtr)
	randomized_select((GPTR *)varray3,i-1,tnplatforms-1,1,
			  LOGR_is_accessb_lower,LOGR_is_accessb_bigger);
    if(mbrowser)
    {
	const char *lbrowser = LOGR_get_user_agent(mbrowser);
	long scount = LOGR_get_browser_naccesses(mbrowser);
	long npages = LOGR_get_browser_npages(mbrowser);
	char str[20], str2[20], str3[20], str4[20],str5[20];
	double ssize = LOGR_get_browser_sizebytes(mbrowser);
	double ave_size = (ssize/tsize)*100.0;
	float ave_nacc = ((float)scount/(float)tacc)*100.0;
	sprintf(str,"%ld",scount);
	sprintf(str5,"%ld",npages);
	sprintf(str2,"%.2f K",ssize/1024.0);
	sprintf(str3,"%.2f %s",ave_size,"&#37;"); 
	sprintf(str4,"%.2f %s",ave_nacc,"&#37;"); 
	write_row_string5(fp,lbrowser,str,str4,str5,str2,str3);
    }
  }
  write_end_table_html_file(fp); }

  if(tnspiders > 0) {
      if(nuas < tnspiders)
	  tn = nuas;
      else
	  tn = tnspiders;

  sprintf(ntitle,"%d Spiders with Most Access Count", tn);
  write_header_html_file(fp,2,ntitle);
  write_start_simple_table_html_file(fp);
  write_header_table_string5(fp,"Spider","Access Count",
			     "Percentage Access Count", "HTML Pages",
			     "Bytes","Percentage Bytes");
  for(i = 1; i<=tn; i++)
  {
    WebBrowserNodePtr mbrowser= (WebBrowserNodePtr)
	randomized_select((GPTR *)varray4,i-1,tnspiders-1,1,
			  LOGR_is_accessb_lower,LOGR_is_accessb_bigger);
    if(mbrowser)
    {
	const char *lbrowser = LOGR_get_user_agent(mbrowser);
	long scount = LOGR_get_browser_naccesses(mbrowser);
	long npages = LOGR_get_browser_npages(mbrowser);
	char str[20], str2[20], str3[20], str4[20],str5[20];
	double ssize = LOGR_get_browser_sizebytes(mbrowser);
	double ave_size = (ssize/tsize)*100.0;
	float ave_nacc = ((float)scount/(float)tacc)*100.0;
	sprintf(str,"%ld",scount);
	sprintf(str5,"%ld",npages);
	sprintf(str2,"%.2f K",ssize/1024.0);
	sprintf(str3,"%.2f %s",ave_size,"&#37;"); 
	sprintf(str4,"%.2f %s",ave_nacc,"&#37;"); 
	write_row_string5(fp,lbrowser,str,str4,str5,str2,str3);
    }
  }
  write_end_table_html_file(fp); }

  if(tnuas > 0) {
      
  if(nuas < tnuas)
      tn = nuas;
  else
      tn = tnuas;

  sprintf(ntitle,"%d Specific Browsers with Most Access Count", tn);
  write_header_html_file(fp,2,ntitle);
  write_start_simple_table_html_file(fp);
  write_header_table_string5(fp,"Browser","Access Count",
			     "Percentage Access Count", "HTML Pages",
			     "Bytes","Percentage Bytes");
  for(i = 1; i<=tn; i++)
  {
    WebBrowserNodePtr mbrowser= (WebBrowserNodePtr)
	randomized_select((GPTR *)varray,i-1,tnuas-1,1,
			  LOGR_is_accessb_lower,LOGR_is_accessb_bigger);
    if(mbrowser)
    {
	const char *lbrowser = LOGR_get_user_agent(mbrowser);
	long scount = LOGR_get_browser_naccesses(mbrowser);
	long npages = LOGR_get_browser_npages(mbrowser);
	char str[20], str2[20], str3[20], str4[20],str5[20];
	double ssize = LOGR_get_browser_sizebytes(mbrowser);
	double ave_size = (ssize/tsize)*100.0;
	float ave_nacc = ((float)scount/(float)tacc)*100.0;
	sprintf(str,"%ld",scount);
	sprintf(str5,"%ld",npages);
	sprintf(str2,"%.2f K",ssize/1024.0);
	sprintf(str3,"%.2f %s",ave_size,"&#37;"); 
	sprintf(str4,"%.2f %s",ave_nacc,"&#37;"); 
	write_row_string5(fp,lbrowser,str,str4,str5,str2,str3);
    }
  }
  write_end_table_html_file(fp); }


  free(varray); varray = NULL;
  free(varray2); varray2 = NULL;
  free(varray3); varray3 = NULL;
  free(varray4); varray4 = NULL;
}

int 
report_spages_most_requested(GraphFrame *gf, char *name_file,int npages,
			     WebStatPtr msn)
{
  FILE *fp = assert_open_html_file(name_file);
  if(!fp)
      return -1;
  write_head_html_file(fp,"Most Requested Pages");
  write_start_body_html_file(fp,"#ffffff");
  write_body_spmreq_html_file(gf,fp,npages,msn);
  write_end_body_html_file(fp);
  write_end_html_file(fp);
  fclose(fp);
  return 0;
}

void
write_body_spmreq_html_file(GraphFrame *gf,FILE *fp,int npages,WebStatPtr msn)
{
  LNode_type *lips = LOGR_get_list_simple_pages(msn);
  LNode_type *ptr = NULL;
  WebSimplePagePtr *varray = NULL;
  WebSimplePagePtr *varray2 = NULL;
  WebSimplePagePtr *varray3 = NULL;
  int i=0,j=0,k=0;
  int tnpages=0;
  char ntitle[100];
  char *scolor = "beige";

  time_t now;

  long thits = LOGR_get_total_site_naccesses(msn);
  int nnhtml = 0;
  int nhtml = 0;
  int nobj = 0;

  for(ptr = lips->back; ptr != lips; ptr=ptr->back)
  {
    WebSimplePagePtr msp = (WebSimplePagePtr)ptr->info;

    if(LOGR_is_simple_page_object(msp))
	nobj++;
    else if (LOGR_is_simple_page_html(msp))
	nhtml++;
    else
	nnhtml++;
  }

  varray = nhtml ? (WebSimplePagePtr *)calloc(nhtml,sizeof(WebSimplePagePtr)) : NULL;

  if(!varray && nhtml > 0) {
      fprintf(stderr,"Out of Memory write_body_spmreq_html_file\n");
      abort();
  }

  varray2 = nnhtml ? (WebSimplePagePtr *)calloc(nnhtml,sizeof(WebSimplePagePtr)) : NULL;

  if(!varray2 && nnhtml > 0) {
      fprintf(stderr,"Out of Memory write_body_spmreq_html_file\n");
      abort();
  }

  varray3 = nobj ? (WebSimplePagePtr *)calloc(nobj,sizeof(WebSimplePagePtr)) : NULL;

  if(!varray3 && nobj > 0) {
      fprintf(stderr,"Out of Memory write_body_spmreq_html_file\n");
      abort();
  }

  for(ptr = lips->back; ptr != lips; ptr=ptr->back)
  {
    WebSimplePagePtr msp = (WebSimplePagePtr)ptr->info;

    if(LOGR_is_simple_page_object(msp))
	varray3[k++] = msp;
    else if (LOGR_is_simple_page_html(msp))
	varray[i++] = msp;
    else
	varray2[j++] = msp;
  }

  now = time(NULL);
  write_header_html_file(fp,1,"List of Most Requested Pages");
  write_bold_html_file(fp,"Last Update :");
  fprintf(fp, " %s",ctime(&now));
  write_break_html_file(fp);
  write_bold_html_file(fp,"Time Periods : ");
  write_break_html_file(fp);
  write_time_periods(fp,msn);
  write_bold_html_file(fp,"Total Number of Requests : ");
  fprintf(fp, " %ld \n",thits);
  write_horizontal_rule_html_file(fp);
  if(nobj > 0)
      fprintf(fp,RMRPString);
  else
      fprintf(fp,RMRPString2);
  write_horizontal_rule_html_file(fp);

  if(nhtml > 0) {
  if(npages > nhtml)
      tnpages = nhtml;
  else
      tnpages = npages;

  sprintf(ntitle,"%d Most Requested HTML Pages", tnpages);
  write_header_html_file(fp,2,ntitle);
  write_start_simple_table_html_file(fp);
  write_header_table_string4(fp,"Num", "URL","Hits","Percentage","Size");

  for(i = 1; i<=tnpages ; i++)
  {
    WebSimplePagePtr msp = (WebSimplePagePtr)
	randomized_select((GPTR *)varray,i-1,nhtml-1,1,
			  LOGR_is_access_simple_lower,
			  LOGR_is_access_simple_bigger);
    if(msp)
    {
      const char *url = LOGR_get_simple_url(msp);
      const char *title = LOGR_get_simple_title(msp);
      long nacc = LOGR_get_simple_naccesses(msp);
      long sbytes = LOGR_get_simple_sizebytes(msp);
      float ave_nacc = ((float)nacc/(float)thits)*100.0;
      char snum[20],scount[20],save[20],ssbytes[20];
      sprintf(snum,"%d",i);
      sprintf(ssbytes,"%ld",sbytes);
      sprintf(scount,"%ld",nacc);
      sprintf(save,"%.2f %s",ave_nacc,"&#37;");
      if(LOGR_is_simple_page_independent(msp))
	  write_row_num_link_title_string3_color(fp,snum,url,title,scount,
						 save,ssbytes,scolor);
      else
	  write_row_num_link_title_string3_color(fp,snum,url,title,scount,
						 save,ssbytes,NULL);

    }	
  }
  write_end_table_html_file(fp); }


  if(nnhtml > 0) {
  if(npages > nnhtml)
      tnpages = nnhtml;
  else
      tnpages = npages;

  sprintf(ntitle,"%d Most Requested Non HTML Pages", tnpages);
  write_header_html_file(fp,2,ntitle);
  write_start_simple_table_html_file(fp);
  write_header_table_string4(fp,"Num", "URL","Hits","Percentage","Size");

  for(i = 1; i<=tnpages ; i++)
  {
    WebSimplePagePtr msp = (WebSimplePagePtr)
	randomized_select((GPTR *)varray2,i-1,nnhtml-1,1,
			  LOGR_is_access_simple_lower,
			  LOGR_is_access_simple_bigger);
    if(msp)
    {
      const char *url = LOGR_get_simple_url(msp);
      long nacc = LOGR_get_simple_naccesses(msp);
      long sbytes = LOGR_get_simple_sizebytes(msp);
      float ave_nacc = ((float)nacc/(float)thits)*100.0;
      char snum[20],scount[20],save[20],ssbytes[20];
      sprintf(snum,"%d",i);
      sprintf(ssbytes,"%ld",sbytes);
      sprintf(scount,"%ld",nacc);
      sprintf(save,"%.2f %s",ave_nacc,"&#37;");

      if(LOGR_is_simple_page_independent(msp))
	  write_row_num_link_string3_color(fp,snum,url,scount,save,ssbytes,
					   scolor);
      else
	  write_row_num_link_string3_color(fp,snum,url,scount,save,ssbytes,
					   NULL);
    }	
  }
  write_end_table_html_file(fp); }

  if(nobj > 0) {
  if(npages > nobj)
      tnpages = nobj;
  else
      tnpages = npages;

  sprintf(ntitle,"%d Most Requested Inline Objects", tnpages);
  write_header_html_file(fp,2,ntitle);
  write_start_simple_table_html_file(fp);
  write_header_table_string4(fp,"Num", "URL","Hits","Percentage","Size");

  for(i = 1; i<=tnpages ; i++)
  {
    WebSimplePagePtr msp = (WebSimplePagePtr)
	randomized_select((GPTR *)varray3,i-1,nobj-1,1,
			  LOGR_is_access_simple_lower,
			  LOGR_is_access_simple_bigger);
    if(msp)
    {
      const char *url = LOGR_get_simple_url(msp);
      long nacc = LOGR_get_simple_naccesses(msp);
      long sbytes = LOGR_get_simple_sizebytes(msp);
      float ave_nacc = ((float)nacc/(float)thits)*100.0;
      char snum[20],scount[20],save[20],ssbytes[20];
      sprintf(snum,"%d",i);
      sprintf(ssbytes,"%ld",sbytes);
      sprintf(scount,"%ld",nacc);
      sprintf(save,"%.2f %s",ave_nacc,"&#37;");

      if(LOGR_is_simple_page_independent(msp))
	  write_row_num_link_string3_color(fp,snum,url,scount,save,ssbytes,
					   scolor);
      else
	  write_row_num_link_string3_color(fp,snum,url,scount,save,ssbytes,
					   NULL);
    }	
  }
  write_end_table_html_file(fp); }

  free(varray); varray = NULL;
  free(varray2); varray2 = NULL;
  free(varray3); varray3 = NULL;
}


